package com.efast.cafe.portal.service.query.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.support.json.JSONUtils;
import com.efast.cafe.framework.constant.CommonConstant;
import com.efast.cafe.framework.redis.ObjectRedisTemplate;
import com.efast.cafe.portal.bean.query.TableConditionBean;
import com.efast.cafe.portal.bean.query.TableOpBean;
import com.efast.cafe.portal.bean.query.TemplateObjBean;
import com.efast.cafe.portal.dao.query.QueryTemplateDao;
import com.efast.cafe.portal.entity.query.TableView;
import com.efast.cafe.portal.entity.query.Template;
import com.efast.cafe.portal.entity.query.TemplateCondition;
import com.efast.cafe.portal.entity.query.TemplateJsonObj;
import com.efast.cafe.portal.entity.query.TemplateSave;
import com.efast.cafe.portal.service.query.IRedisResourceService;
import com.efast.cafe.util.JsonUtil;
import com.efast.cafe.util.SetUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@SuppressWarnings("all")
@Service
@Transactional
public class RedisResourceServiceImpl implements IRedisResourceService {
	@Autowired
	private QueryTemplateDao queryTemplateDao;
	
	@Autowired
	private ObjectRedisTemplate objectRedisTemplate;	
	
	private static ValueOperations<String,Object> redislistObj;
	
	@Override
	public List<TableConditionBean> queryTableConditionBean(Integer templateId) {
		initRedislistObj();
		Object object = redislistObj.get(CommonConstant.REDIS_PLATEFORM_TABLE_CONDITION_LIST_TEMPLATEID+templateId);
		if(null==object){
			List<TableConditionBean> list = queryTemplateDao.selecetTableConditionListByTemplateId(templateId);
			redislistObj.set(CommonConstant.REDIS_PLATEFORM_TABLE_CONDITION_LIST_TEMPLATEID+templateId, list);
			return list;
		}else{
			return (List<TableConditionBean>)object;
		}
	}
	
	@Override
	public List<TableView> queryTableView(Integer templateId) {
		initRedislistObj();
		Object object = redislistObj.get(CommonConstant.REDIS_PLATEFORM_TABLE_VIEW_LIST_TEMPLATEID+templateId);
		if(null==object){
			List<TableView> list = queryTemplateDao.selectTableViewListByTemplateId(templateId);
			redislistObj.set(CommonConstant.REDIS_PLATEFORM_TABLE_VIEW_LIST_TEMPLATEID+templateId, list);
			return list;
		}else{
			return (List<TableView>)object;
		}
	}
	
	@Override
	public List<TableOpBean> queryTableOp(Integer id) {
		initRedislistObj();
		Object object = redislistObj.get(CommonConstant.REDIS_PLATEFORM_TABLE_OP_LIST_CONDITIONID+id);
		if(null==object){
			List<TableOpBean> list = queryTemplateDao.selectTableOpListByConditionId(id);
			redislistObj.set(CommonConstant.REDIS_PLATEFORM_TABLE_OP_LIST_CONDITIONID+id, list);
			return list;
		}else{
			return (List<TableOpBean>)object;
		}
	}
	
	@Override
	public Template getTemplateById(Integer id) {
		initRedislistObj();
		Object object = redislistObj.get(CommonConstant.REDIS_PLATEFORM_TEMPLATE_ID+id);
		if(null==object){
			Template list = queryTemplateDao.selectTemplateDetailById(id);
			if (list != null) {
				redislistObj.set(CommonConstant.REDIS_PLATEFORM_TEMPLATE_ID+id, list);
			}
			return list;
		}else{
			return (Template)object;
		}
	}
	
	@Override
	public Template getTemplateByCode(String code) {
		String id = "0";
		String [] arrays = code.split("-");
		if (arrays.length > 1) {
			id = arrays[1];
		}
		Integer templateId = null;
		try {
			templateId = Integer.parseInt(id);
		} catch (Exception e) {
			return null;
		}
		return getTemplateById(templateId);
	}

	@Override
	public void updateTemplate(Template template,TemplateObjBean bean) {
		initRedislistObj();
		if (template != null && new Integer(1).equals(template.getIsDelete())) {
			clearRedis(CommonConstant.REDIS_PLATEFORM_TEMPLATE_ID+template.getId());
			clearRedis(CommonConstant.REDIS_PLATEFORM_TEMPLATE_CODE+template.getId());
			return;
		}
		redislistObj.set(CommonConstant.REDIS_PLATEFORM_TEMPLATE_ID+template.getId(),template);
		updateTemplateObjBean(bean, template.getId());
	}

	@Override
	public void updateTableOp(List<TableOpBean> beans,Integer tableConditionId) {
		initRedislistObj();
		if (SetUtil.isEmpty(beans)) {
			clearRedis(CommonConstant.REDIS_PLATEFORM_TABLE_OP_LIST_CONDITIONID+tableConditionId);
			return;
		}
		redislistObj.set(CommonConstant.REDIS_PLATEFORM_TABLE_OP_LIST_CONDITIONID+tableConditionId,beans);
	}

	@Override
	public void updateTableView(List<TableView> tableView,Integer templateId) {
		initRedislistObj();
		if (SetUtil.isEmpty(tableView)) {
			clearRedis(CommonConstant.REDIS_PLATEFORM_TABLE_VIEW_LIST_TEMPLATEID+templateId);
			return;
		}
		redislistObj.set(CommonConstant.REDIS_PLATEFORM_TABLE_VIEW_LIST_TEMPLATEID + templateId,tableView);
	}

	@Override
	public void updateTableCondition(List<TableConditionBean> beans,Integer templateId) {
		initRedislistObj();
		if (SetUtil.isEmpty(beans)) {
			clearRedis(CommonConstant.REDIS_PLATEFORM_TABLE_CONDITION_LIST_TEMPLATEID+templateId);
			return;
		}
		redislistObj.set(CommonConstant.REDIS_PLATEFORM_TABLE_CONDITION_LIST_TEMPLATEID+templateId,beans);
		
	}

	@Override
	public void updateTemplateSave(TemplateSave templateSave,Integer templateId) {
		initRedislistObj();
		if (templateSave == null) {
			clearRedis(CommonConstant.REDIS_PLATEFORM_TABLE_VIEW_LIST_TEMPLATEID+templateId);
			return;
		}
		redislistObj.set(CommonConstant.REDIS_PLATEFORM_TEMPLATE_SAVE_ID+templateSave.getId(),templateSave);
	}

	@Override
	public TemplateSave queryTemplateSaveBean(Integer templateSaveId) {
		initRedislistObj();
		Object object = redislistObj.get(CommonConstant.REDIS_PLATEFORM_TEMPLATE_SAVE_ID+templateSaveId);
		if(null==object){
			try {
			TemplateJsonObj list = queryTemplateDao.queryTemplateSave(templateSaveId);
			TemplateSave save = new TemplateSave();
			List<TemplateCondition> condition;
			condition = (List<TemplateCondition>) JsonUtil.toList(TemplateCondition[].class, list.getTableConditionRef());
			save.setCode(list.getCode());
			save.setQueryTemplateId(list.getQueryTemplateId());
			save.setTableConditionJson(list.getConditionJson());
			save.setTableConditionRef(list.getTableConditionRef());
			save.setConditions(condition);
			redislistObj.set(CommonConstant.REDIS_PLATEFORM_TEMPLATE_SAVE_ID+templateSaveId, save);
			return save;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}else{
			return (TemplateSave)object;
		}
	}
	
	public TemplateObjBean geTemplateObjBean(Integer templateId) {
		initRedislistObj();
		Object object = redislistObj.get(CommonConstant.REDIS_PLATEFORM_TEMPLATE_CODE+templateId);
		if(null==object){
			TemplateObjBean bean = new TemplateObjBean();
			List<TableView> tableViews = queryTableView(templateId);
			Template template = getTemplateById(templateId);
			List<TableConditionBean> tableConditions = queryTableConditionBean(templateId);
			bean.setTemplate(template);
			if (!SetUtil.isEmpty(tableConditions)) {
				for (TableConditionBean tableConditionBean : tableConditions) {
					tableConditionBean.setTableOps(queryTableOp(tableConditionBean.getId()));
				}
			}
			bean.setTableConditions(tableConditions);
			bean.setTableViews(tableViews);
			return bean;
		} else {
			return (TemplateObjBean)object;
		}
	}
	
	public void updateTemplateObjBean(TemplateObjBean template,Integer templateId) {
		initRedislistObj();
		redislistObj.set(CommonConstant.REDIS_PLATEFORM_TEMPLATE_CODE+templateId,template);
	}
	
	public void updateTemplateLayoutBean(TemplateObjBean templateObjBean,String formLayout, Integer templateId) {
		initRedislistObj();
		if(templateObjBean!=null){
			Template template = templateObjBean.getTemplate() ;
			template.setFormLayout(formLayout);
			redislistObj.set(CommonConstant.REDIS_PLATEFORM_TEMPLATE_ID+templateId,template);
			templateObjBean.setTemplate(template); 
			redislistObj.set(CommonConstant.REDIS_PLATEFORM_TEMPLATE_CODE+templateId,templateObjBean);
		}
	}

	private void clearRedis(String key) {
		List<String> redisKeys = new ArrayList<>();
		Set<String> keys11 = objectRedisTemplate.keys(key);
		if (!SetUtil.isEmpty(keys11)) {
			for(String s : keys11){redisKeys.add(s);}
			if (!SetUtil.isEmpty(redisKeys)) {
				objectRedisTemplate.delete(redisKeys);
			}
		}		
	}

	public ValueOperations<String, Object> getRedislistObj() {
		redislistObj = objectRedisTemplate.opsForValue();
		return redislistObj;
	}

	public void setRedislistObj(ValueOperations<String, Object> redislistObj) {
		this.redislistObj = redislistObj;
	}
	public void initRedislistObj () {
		if (redislistObj == null) {
			redislistObj = objectRedisTemplate.opsForValue();
		}
		
	}
	
}
